package com.wxyzz.canteenadmin.shop.employee.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxyzz.canteenadmin.shop.employee.VO.EmpUploadVO;
import com.wxyzz.canteenadmin.shop.employee.VO.EmployeeVO;
import com.wxyzz.canteenadmin.shop.employee.mapper.EmployeeMapper;
import com.wxyzz.canteenadmin.shop.employee.param.AddEmpParam;
import com.wxyzz.canteenadmin.shop.employee.param.EmployeePageParam;
import com.wxyzz.canteenadmin.shop.employee.service.EmployeeService;
import com.wxyzz.canteencommon.entity.shop.employee.Employee;
import com.wxyzz.canteencommon.exception.CustmerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * --- 加油，努力 ---
 *
 * @Author XYP
 * @Date 2024/10/10
 * @Solgne 从想做到能做
 * @Desc
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    //分页和模糊查询
    @Override
    public PageInfo<EmployeeVO> page(EmployeePageParam employeePageParam) {
        PageHelper.startPage(employeePageParam.getPageNum(), employeePageParam.getPageSize());
        List<EmployeeVO> employeeList = employeeMapper.page(employeePageParam.getType(),employeePageParam.getName());
        PageInfo<EmployeeVO> employeePageInfo = new PageInfo<>(employeeList);
        return employeePageInfo;
    }

    @Override
    public Employee findById(Integer id) {
        return employeeMapper.selectById(id);
    }

    //删除
    @Override
    public void deleteById(Integer id) {
        employeeMapper.delById(id);
    }
    //批量删除
    @Override
    public void delBatch(List<Integer> ids) {
        employeeMapper.delBatch(ids);
    }
    //添加
    @Override
    public void add(AddEmpParam addEmpParam) {
        if (addEmpParam.getType()==null){
            throw new CustmerException(500,"员工类型未选择...");
        }
        if (addEmpParam.getName()==null){
            throw new CustmerException(500,"员工姓名未填写...");
        }
        if (addEmpParam.getPhoneNumber()==null){
            throw new CustmerException(500,"手机号未填写...");
        }
        if (addEmpParam.getBelongingStoreId()==null){
            throw new CustmerException(500,"所属店铺未选择...");
        }
        employeeMapper.insert(addEmpParam);
    }
    //更新
    @Override
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }
    //导入
    @Override
    public void importUnit(MultipartFile file) {
        /**
         * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
         */
        int BATCH_COUNT = 100;
        /**
         * 缓存的数据
         */
        List<EmpUploadVO> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        EasyExcel.read(inputStream,EmpUploadVO.class,new ReadListener<EmpUploadVO>(){
            //每读取一条数据就调用invoke方法
            @Override
            public void invoke(EmpUploadVO employee, AnalysisContext analysisContext) {
                System.out.println(employee);
                cachedDataList.add(employee);
                if (cachedDataList.size() == 3){
                    employeeMapper.insertBatch(cachedDataList);
                    cachedDataList.clear();
                }
            }
            //解析完成之后调用的方法
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("解析完毕之后");
                //剩余的数据也需要插入数据库
                if (cachedDataList.size() > 0) {
                    employeeMapper.insertBatch(cachedDataList);
                }
            }
        }).sheet().doRead();
    }
    //导出
    @Override
    public void exportExcel(HttpServletResponse response) {
        List<EmployeeVO> employeeVOS = employeeMapper.page(null, null);
        if (employeeVOS == null){
            throw new RuntimeException("数据不存在");
        }
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            String fileName = "员工数据";
            fileName = URLEncoder.encode(fileName, "utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(),EmployeeVO.class).sheet("模板").doWrite(employeeVOS);
        } catch (IOException e) {
        }

    }
}
